package com.yndsoft.jc.supplier.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.supplier.model.OrderDetailModel.DetsBean;
import com.yndsoft.jc.supplier.order.EntryBox;
import com.yndsoft.jc.supplier.order.OnClickPositionListener;
import com.yndsoft.jc.supplier.order.OnDeletePositionListener;
import com.yndsoft.jc.supplier.order.entry.OrderEntryActivity;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerAdapter;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerHolder;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerListener;

import java.util.ArrayList;
import java.util.List;

public class OrderDetailAdapter extends RecyclerAdapter<DetsBean> implements RecyclerListener.OnClickListener<RecyclerHolder> {

    private Activity activity;
    private String tag;
    private List<String> imgPath;
    private OnClickPositionListener onClickPositionListener;
    private OnDeletePositionListener onDeletePositionListener;
    private String resultValue = null;
    private int resultPosition = -1;

    public void setOnClickPositionListener(OnClickPositionListener onClickPositionListener) {
        this.onClickPositionListener = onClickPositionListener;
    }

    public void setOnDeletePositionListener(OnDeletePositionListener onDeletePositionListener) {
        this.onDeletePositionListener = onDeletePositionListener;
    }

    public OrderDetailAdapter(Context context, Activity activity, List<DetsBean> data, String tag) {
        super(context, data, R.layout.item_order_detail);
        this.activity = activity;
        this.tag = tag;
    }

    @Override
    public void convert(final RecyclerHolder holder, final DetsBean model) {
        DetsBean.ProductBean product = model.getProduct();
        if (product != null) {
            holder.setTvText(R.id.order_detail_title, product.getName() + product.getSubname());
        }
        DetsBean.ProductattributeBean productattribute = model.getProductattribute();
        if (productattribute != null) {
            holder.setTvText(R.id.order_detail_content, productattribute.getProject() + " | " + productattribute.getMethod() + " | " + productattribute.getStandard());
        }
        TextView orderDetailResult = holder.findViewById(R.id.order_detail_result);
        if (holder.position == resultPosition && imgPath == null) {
            if (resultValue != null) {
                orderDetailResult.setText(resultValue);
                resultValue = null;
                resultPosition = -1;
            }
        }
        RecyclerView orderDetailImg = holder.findViewById(R.id.order_detail_img);
        List<String> list = new ArrayList<>();
        DetsBean.SupplierresultBean supplierresult = model.getSupplierresult();
        if (supplierresult != null) {
            orderDetailResult.setText(supplierresult.getNote() != null ? supplierresult.getNote() : "");
            List<DetsBean.SupplierresultBean.ImgsBean> imgs = supplierresult.getImgs();
            if (imgs != null && imgs.size() > 0) {
                for (int i = 0; i < imgs.size(); i++) {
                    DetsBean.SupplierresultBean.ImgsBean imgsBean = imgs.get(i);
                    if (imgsBean != null) {
                        list.add(imgsBean.getImg());
                    }
                }
            }
        }
        if (null != imgPath && imgPath.size() > 0 && resultPosition == -1 && resultValue == null) {
            list.addAll(imgPath);
            imgPath = null;
        }
        if (tag != null && tag.equals(Constants.ENTRY) && !list.contains(Constants.PLACE_HOLDER)) {
            list.add(Constants.PLACE_HOLDER);
        }
        orderDetailImg.setLayoutManager(new GridLayoutManager(mContext, 5));
        OrderDetailImgAdapter orderDetailImgAdapter = new OrderDetailImgAdapter(mContext, activity, list, tag, holder.position, model.getId());
        orderDetailImg.setAdapter(orderDetailImgAdapter);
        orderDetailImgAdapter.setOnClickPositionListener(new OnClickPositionListener() {
            @Override
            public void onClickPosition(int position, String oDetId) {
                if (onClickPositionListener != null) {
                    onClickPositionListener.onClickPosition(position, oDetId);
                }
            }
        });
        orderDetailImgAdapter.setOnDeletePositionListener(new OnDeletePositionListener() {
            @Override
            public void onDeletePosition(int parentPosition, int childPosition) {
                if (onDeletePositionListener != null) {
                    onDeletePositionListener.onDeletePosition(parentPosition, childPosition);
                }
            }
        });

        holder.setOnClickListener(this, R.id.order_detail_result, R.id.order_detail_root);
    }

    public void setImgPath(List<String> path) {
        imgPath = new ArrayList<>();
        imgPath.addAll(path);
    }

    public void setResult(String resultValue, int resultPosition) {
        this.resultValue = resultValue;
        this.resultPosition = resultPosition;
        notifyItemChanged(resultPosition);
    }

    @Override
    public void onClick(View v, int position, RecyclerHolder holder) {
        switch (v.getId()) {
            case R.id.order_detail_result:
                if (tag != null && tag.equals(Constants.ENTRY)) {
                    TextView result = v.findViewById(R.id.order_detail_result);
                    DetsBean detsBean = mData.get(position);
                    if (detsBean != null) {
                        EntryBox.start(mContext, position, result.getText().toString(), detsBean.getId());
                    }
                }
                break;
            case R.id.order_detail_root:
                if (tag != null && tag.equals(Constants.ENTRY)) {
//                    OrderEntryActivity.start(mContext, "1");
                }
                break;
        }
    }
}
